
package com.lht.admin.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lht.admin.annotation.SysLog;
import com.lht.admin.constants.DataValidConstant;
import com.lht.admin.entity.TargetEntity;
import com.lht.admin.service.TargetService;
import com.lht.admin.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 目标管理表Controller
 *
 * @author ZhengZhanWu
 * @date 2020-01-14 23:42:37
 */
@Slf4j
@RestController
@RequestMapping("/target")
public class TargetController extends AbstractController {
    @Autowired
    private TargetService targetService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return R
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("target:list")
    public R queryAll(@RequestParam Map<String, Object> params) {
        params.put("isDelete", DataValidConstant.IS_DELETE_FALSE_STR);
        List<TargetEntity> list = targetService.queryAll(params);

        return R.ok().put("list", list);
    }

    /**
     * 分页查询目标管理表
     *
     * @param params 查询参数
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("target:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("isDelete", DataValidConstant.IS_DELETE_FALSE_STR);
        Page page = targetService.queryPage(params);

        return R.ok().put("page", parseToPageUtils(page));
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return R
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("target:info")
    public R info(@PathVariable("id") Long id) {
        TargetEntity target = targetService.getById(id);

        return R.ok().put("target", target);
    }

    /**
     * 新增目标管理表
     *
     * @param target target
     * @return R
     */
    @SysLog("新增目标管理表")
    @RequestMapping("/save")
    @RequiresPermissions("target:save")
    public R save(@RequestBody TargetEntity target) {

        log.info("Function[save] save target:{}", JSON.toJSONString(target));

        targetService.add(getUser(), target);

        return R.ok();
    }

    /**
     * 修改目标管理表
     *
     * @param target target
     * @return R
     */
    @SysLog("修改目标管理表")
    @RequestMapping("/update")
    @RequiresPermissions("target:update")
    public R update(@RequestBody TargetEntity target) {

        targetService.update(target);

        return R.ok();
    }

    /**
     * 根据主键删除目标管理表
     *
     * @param ids ids
     * @return R
     */
    @SysLog("删除目标管理表")
    @RequestMapping("/delete")
    @RequiresPermissions("target:delete")
    public R delete(@RequestBody Long[] ids) {
        targetService.deleteBatch(getUser(),ids);

        return R.ok();
    }
}
